import g_get from '@dfeidao/atom-web/global/get';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import dsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import render from '@dfeidao/atom-web/render/render';
import { systemid } from '../../atom/config';
import hide_loading from '../../atom/hide-loading';
import show_loading from '../../atom/show-loading';
import whoami from '../../atom/whoami';

import p002 from './p002';
import p003 from './p003';
interface Aa {
	docDataStore: {
		audit_list: {
			dataArray: [];
			summary: {
				totalRow: number
			}
		}
	};
}
interface IReview {
	comment_numbering: string;
	comments: string;
	productid: string;
	commentator_numbering: string;
	dynamic_id: string;
	id: string;
	commentator_time: number;
	commentator_time_str: string;
	staff_name: string;
	headimg: string;
	statistical_magnitude: number;
	isa: number;
	floor: number;
}
function trim(str: string) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}  // 去空格
export default async function a002(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLElement }) {
	const page_name = fd.data.node.getAttribute('page-name');
	const global_dynamic_head_zjc4 = g_get('global_dynamic_head_zjc4');
	const global_asso_type_zjc4 = g_get('global_asso_type_zjc4');
	const user = await whoami(fd);
	const config = user.params as { productid: string, staff_no: string; staff_name: string; deptid: string; };
	const productid = config.productid;
	const staff_no = config.staff_no;
	const staff_name = config.staff_name;
	// 发表评论.
	const comments = fd.data.node.querySelector<HTMLSelectElement>('[id="comment"]').value;
	if (page_name === 'grab-details') {
		const active_state = g_get('grab_details_active_state');
		if (active_state === '200') {
			return error('活动未开始');
		}
	}
	if (!trim(comments)) {
		return error('请输入发表内容');
	}
	const commentator_time = new Date().getTime();
	const dynamic_id = get(fd, 'dynamic_id');
	const commentator_numbering = get(fd, 'staff_no');
	try {
		show_loading();

		const res = await nodejs(page_name + '/s003', { commentator_time, productid, commentator_numbering, comments, dynamic_id, staff_no, staff_name });
		const conditions = {
			"param": {
				"staff_no": commentator_numbering,
				"administrator": '',
				"asso_type": global_asso_type_zjc4,
				"asso_title": global_dynamic_head_zjc4,
				"asso_no": 'LX1001',
				"productid": productid
			}
		};
		const res2: Aa = await dsd(systemid, '', 'kinth_oadsd0000019', conditions);
		if (res2) { }

		hide_loading();
		if (res === 'success') {
			success('发表成功');
			fd.data.node.querySelector<HTMLSelectElement>('[id="comment"]').value = '';
			let auth_code = '';
			if (page_name === 'depart-details') {
				auth_code = g_get('depart_details_auth_code');
			} else if (page_name === 'sign-details') {
				auth_code = g_get('sign_details_auth_code');
			} else if (page_name === 'grab-details') {
				auth_code = g_get('grab_details_auth_code');
			} else if (page_name === 'lottery-details') {
				auth_code = g_get('lottery_details_auth_code');
			}
			const page_no = 1;
			const query = {
				dynamic_id,
				page_no,
				page_size: 5,
				employee_no: commentator_numbering,
				auth_code,
				productid
			};
			set(fd, 'page_no', query.page_no);
			set(fd, 'page_size', query.page_size);
			const result = await nodejs<{ dataArray: IReview[], count: number, page_no: number, page_size: number; isgrab: number; }>(page_name + '/s002', query);
			if (result.count > 0) {
				fd.data.node.querySelector<HTMLDivElement>('[data-comment="no-comment"]').classList.add('display-n');
				if (result.count <= query.page_size) {
					fd.data.node.querySelector<HTMLDivElement>('[data-id="openmore"]').classList.add('display-n');
				} else {
					fd.data.node.querySelector<HTMLDivElement>('[data-id="openmore"]').classList.remove('display-n');
				}
				fd.data.node.querySelector<HTMLDivElement>('[data-comment="comment"]').classList.remove('display-n');
				if (page_name === 'grab-details') {
					result.isgrab = 1;
				} else {
					result.isgrab = 0;
				}
				result.dataArray.forEach((a, index) => {
					a.floor = (index + 1) + (page_no - 1) * 5;
				});
			} else {
				fd.data.node.querySelector<HTMLDivElement>('[data-comment="comment"]').classList.add('display-n');
				fd.data.node.querySelector<HTMLDivElement>('[data-id="openmore"]').classList.add('display-n');
			}
			render(fd, result, p002, 'p002', 'inner');
			for (let i = 0; i < result.dataArray.length; i++) {
				render(fd, result.dataArray[i], p003, 'p003' + result.dataArray[i].comment_numbering, 'inner');
			}
		} else {
			error('发表失败');
		}
	} catch (e) {
		hide_loading();
		error('发表失败');
	}
}
